#!/usr/bin/perl

#my($in1) = $1;

my($basename) = $ARGV[0];
my($inOther) = "$basename.es.txt";
my($inRussian) = "$basename.ru.txt";

#print "basename $basename / russian file is $inRussian";


open(INPUTOther, "<$inOther"); # open for input
open(INPUTRussian, "<$inRussian"); # open for input


#my(@linesOther) = <INPUTOther>; # read file into list
my @linesOther   = grep { !/^\s*$/ } <INPUTOther>;   # skip empty lines from file
my @linesRussian = grep { !/^\s*$/ } <INPUTRussian>;   # skip empty lines from file
#my(@linesRussian) = <INPUTRussian>; # read file into list

my($lineOth);
my($lineRu);

my($lineNumber);

$lineNumber=0;

#@linesOther =~ s/\n+/\n/g;

print "<html><body>\n";
print "<table border=1>\n";

foreach $lineRu (@linesRussian) # loop thru list
{	
	my($lineEn) = @linesOther[$lineNumber++];
	$lineEn  =~  s/"(.*?)"/``$1''/g; # ungreedy matching quotes and translating them into tex quotes
	
	
	$lineEn  =~ s/ (\.|\?|!)/$1/g;  # removing spaces before punctuation -> . ? !
	$lineRu  =~ s/ (\.|\?|!)/$1/g;  # removing spaces before punctuation -> . ? !
	
	$lineEn  =~ s/\.\.\./\\ldots /g; # replacing elipsis (...) by latex elipsis (\ldots)
	#$lineRu  =~ s/…/ \\ldots /g;         # wikisource has been using a special character for 3 dots / has caused some presentation problems
	
	$lineEn  =~ s/^-| -/ ---/g;			# putting large hyphens in place -- those used when story characters have a line to say
	
	$lineRu  =~ s/«/``/g;	#because xelatex doesn't support this character
	$lineRu  =~ s/»/''/g;	#because xelatex doesn't support this character
	$lineEn  =~ s/«/``/g;	#because xelatex doesn't support this character
	$lineEn  =~ s/»/''/g;	#because xelatex doesn't support this character
	
	
	##########################################################
	# standardize so both xelatex and latex can support these characters
	$lineEn  =~ s/''/{\\closeQuotes}/g;
	$lineEn  =~ s/``/{\\openQuotes}/g;
	$lineRu  =~ s/''/{\\closeQuotes}/g;
	$lineRu  =~ s/``/{\\openQuotes}/g;
	
#	$lineEn  =~ s/---/{\\largeDash}/g;
#	$lineRu  =~ s/---/{\\largeDash}/g;
	

	### heuristics to align paragraphs 
	# the idea is to count how many periods we have in each line
	#my $string = "contando oraciones. uno dos tres. abc. s.";
	my $substring = "\\.|!|ldots|:|\\?|…";
	my $countEs;
	my $countRu;

	#$countEs = ($lineEn =~ tr/$substring//); 
	$countEs = @{[$lineEn =~ /$substring/g]}; 
	$countRu = @{[$lineRu =~ /$substring/g]}; 

	
	print "<tr><td>"; #russian

	

	print "\n$lineRu\n"; 
	print "<br/>$countRu";

	print "</td><td>\n"; # english
	print "\n$lineEn\n";
	print "<br/>$countEs";
	


	
	print "</td></tr>";
	
	
	
	
	
}

print "</table>\n"; 
print "</body></html>\n";

 
close(INPUTOther);
close(INPUTRussian);